'''
关键字驱动的封装，主要用于做selenium常用操作行为的提取和二次封装：
1.get  获取
2.send keys  输入
3.click 点击
4.quit 关闭
'''
from selenium import webdriver
def open_browser(_type):
    try:
        driver=getattr(webdriver,_type)()
    except:
        driver=webdriver.Chrome()
    return driver
class Keys:
    # 创建临时的driver对象
    # driver=webdriver.Chrome()
    def __init__(self,_type):
        self.driver=open_browser(_type)
        self.driver.implicitly_wait(3)

    # 访问URL
    def open(self,url):
        self.driver.get(url)

    # 查找元素
    def locator(self,by,value):
        # by查找方式，value参数具体定位的值
        return self.driver.find_element(by,value)

    # 输入
    def input(self,by,value,txt):
        self.locator(by,value).send_keys(txt)
    # 点击
    def click(self,by,value):
        self.locator(by,value).click()
     # 关闭浏览器
    def quit(self):
        self.driver.quit()

    # 切换句柄
    def switch_window(self,close=None,num=1):
        handles=self.driver.window_handles
        if close is not None:
            self.driver.close()
        self.driver.switch_to.window(handles[num])
